package com.pda.xinmazhi.presenter;

import com.pda.xinmazhi.bean.LatheMachinBean;
import com.pda.xinmazhi.bean.LoginBean;
import com.pda.xinmazhi.bean.SurplusDetailBean;
import com.pda.xinmazhi.bean.request.BackConfirmRequest;
import com.pda.xinmazhi.bean.request.SurplusConfirmRequest;
import com.pda.xinmazhi.contract.BackStorageContract;
import com.pda.xinmazhi.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;

public class BackStoragePresenter extends BasePresenter<BackStorageContract.View> implements BackStorageContract.Presenter{
    @Override
    public void getSurplusDetail(String latheCode) {
        NetUtils.geApi().getSurplusDetail(latheCode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<SurplusDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<SurplusDetailBean> surplusDetailBeanBaseAppEntity) throws Exception {
                        getView().setData(surplusDetailBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void surplusConfirm(SurplusConfirmRequest body) {
        NetUtils.geApi().surplusConfirm(body)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<LoginBean>>() {
                    @Override
                    public void accept(BaseAppEntity<LoginBean> loginBeanBaseAppEntity) throws Exception {
                        getView().showMessage(loginBeanBaseAppEntity.getMessage());
                        getView().onSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void getLatheListByCenter(String centerCode) {
        NetUtils.geApi().getLatheListByCenter(centerCode)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<LatheMachinBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<LatheMachinBean>> surplusDetailBeanBaseAppEntity) throws Exception {
                        getView().setList(surplusDetailBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void backConfirm(BackConfirmRequest body) {
        NetUtils.geApi().backConfirm(body)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<LoginBean>>() {
                    @Override
                    public void accept(BaseAppEntity<LoginBean> loginBeanBaseAppEntity) throws Exception {
                        getView().showMessage(loginBeanBaseAppEntity.getMessage());
                        getView().onSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
